package main.java.org.dss.lm.admin.dao.admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import main.java.org.dss.lm.admin.bean.UserBean;

import com.lm.ledger.dao.ConnMngt1;

public class Test {

	Connection con;
    PreparedStatement pst1 = null;
    ResultSet rs1 = null;
    Integer i = 0;
    long autoUserId;
	public List<UserBean> getUsersInDatabase()
    {
        List<UserBean> usersInDatabase = new ArrayList<UserBean>();
        UserBean userBean;
        PreparedStatement ps;
        ResultSet rs;
        try
        {
            //con=lmDB.getDBConnection();
        	con = ConnMngt1.getConnection();
            pst1=con.prepareStatement("select UserID, JameInsanID, InsanID, Name, Adress, Block, District, State, Country, ContactNumber, RoleID, Status from user where Status = 'Active' order by UserID desc ");
            rs1 = pst1.executeQuery();
            while(rs1.next()) {                
            	String id =rs1.getString("UserID");
            	userBean = new UserBean();
            	userBean.setUserId(rs1.getString("UserID"));
            	userBean.setJiid(rs1.getString("JameInsanID"));
            	userBean.setInsanId(rs1.getString("InsanID"));
            	userBean.setName(rs1.getString("Name"));
            	userBean.setAddress(rs1.getString("Adress"));
            	userBean.setBlock(rs1.getString("Block"));            	
            	userBean.setDistrict(rs1.getString("District"));
            	userBean.setState(rs1.getString("State"));
            	userBean.setCountry(rs1.getString("Country"));
            	userBean.setContactNumber(rs1.getString("ContactNumber"));
            	userBean.setRole(rs1.getString("RoleID"));
            	userBean.setStatus(rs1.getString("Status"));   
            	
            	//userBean.setSelectedGroups(getSelectedGroups(con, id));
                usersInDatabase.add(userBean);
                
                
             //   System.out.println(id);
            }            
        }
        catch(Exception e){e.printStackTrace();}
        finally{try{if(pst1!=null)pst1.close();if(rs1!=null)rs1.close();if(con!=null)con.close();}catch(SQLException ee){}}
        return usersInDatabase;
    }
	
	
	
	 private List getSelectedGroups(Connection con, String userId){
			List groupList = new ArrayList();
		//	System.out.println("user id =="+userId);
			try
	        {
	            
	            pst1=con.prepareStatement("select GroupID from location_user where UserID=?");
	            pst1.setString(1, userId);
	            rs1 = pst1.executeQuery();
	            while(rs1.next()) {               
	            	//System.out.println("rs1.getString(1)   "+rs1.getString(1));
	            	groupList.add(rs1.getString(1));
	            }            
	        }
	        catch(Exception e){e.printStackTrace();}
	        return groupList;
		}
	 
	 public static void main(String[] args) {
		 Test test = new Test();
		List list = test.getUsersInDatabase();
		List list2 = test.getGroupsForUser(list);
		
		
	}
	 private List getGroupsForUser(List userList){
		 UserBean bean;
		 List groupList ;
		 List<UserBean> beans = new ArrayList<UserBean>();
		 Iterator iterator = userList.iterator();
		 while(iterator.hasNext()){
			 bean = (UserBean) iterator.next();
			 String userId = bean.getUserId();
			 System.out.println("userId   "+userId);
			 try {
				con = ConnMngt1.getConnection();
				groupList = new ArrayList();
				pst1=con.prepareStatement("select GroupID from location_user where UserID=?");
				pst1.setString(1, userId);
	            rs1 = pst1.executeQuery();
	            while(rs1.next()) {
	            	
	            	System.out.println("rs1.getString(1)   "+rs1.getString(1));	            	
	            	groupList.add(rs1.getString(1));
	            }  
	            System.out.println(groupList);
				bean.setSelectedGroups(groupList);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			beans.add(bean);
		 }
		 
		 return null;
	 }
}
